﻿#include "DepthStencilBuffer.h"

/////////////////////////////////////////////////////
// UDepthStencilBuffer

UDepthStencilBuffer::UDepthStencilBuffer(const FObjectInitializer& ObjectInitializer)
	: Super(ObjectInitializer)
{
	Width = -1;
	Height = -1;
}

void UDepthStencilBuffer::Resize(int32 InWidth, int32 InHeight)
{
	if (Width != InWidth || Height != InHeight)
	{
		Width = FMath::Max(2, InWidth);
		Height = FMath::Max(2, InHeight);
		
		DepthBuffer.SetNum(Width * Height);
		FMemory::Memzero(DepthBuffer.GetData(), DepthBuffer.Num());
	}
}

void UDepthStencilBuffer::Clear(ESoftRendererDepthBufferType DepthBufferType)
{
	if (DepthBuffer.Num() > 0)
	{
		const float DepthValue = DepthBufferType == ESoftRendererDepthBufferType::ZBuffer ? 1 : 0;
		for (int32 Index = 0, Count = DepthBuffer.Num(); Index < Count; ++Index)
		{
			DepthBuffer[Index] = DepthValue;
		}
	}
}

void UDepthStencilBuffer::SetDepthValue(int32 X, int32 Y, float Value)
{
	if (X >= 0 && X < Width && Y >= 0 && Y < Height)
	{
		DepthBuffer[Y * Width + X] = Value;
	}
}

/////////////////////////////////////////////////////
